<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') {
  $jenis = $_GET['type'];

?>
  <!-- page content -->
  <div class="col" role="main">
    <div class="">
      <div class="clearfix"></div>

      <div class="row">
        <div class="col-md-12">
          <div class="x_panel">
            <div class="x_title" style="text-transform: capitalize;">

              <?php if ($_GET['periode'] == 'y') {
                $tanggal1 = $_POST['tanggal1'];
                $tanggal2 = $_POST['tanggal2'];
                $tgl1 = date('Y-m-d', strtotime($tanggal1));
                $tgl2 = date('Y-m-d', strtotime($tanggal2));
                echo "<h4>Rekap buku besar/Kas periode $tanggal1 sampai $tanggal2</h4>";
              } else {
                $bln = date('m');
                $thn = date('Y');
                echo "<h4>Rekap buku besar/Kas bulan $bln tahun $thn</h4>";
              } ?>



              <div class="clearfix"></div>


              <form action="?module=buku_besar&periode=y" enctype="multipart/form-data" method="POST">


                <div class="well" style="overflow: auto">

                  <div class="col-md-12 col-sm-12 col-xs-12" style="padding: 4px;">
                    <label for="nama">Pilih Periode :</label>
                  </div>

                  <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">

                    <div class="input-group">
                      <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                      <input type="text" autocomplete="off" class="form-control " id="tanggal" required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                                  echo $tanggal1;
                                                                                                                } else {
                                                                                                                  echo "";
                                                                                                                } ?>" name="tanggal1">
                      <div class="input-group-addon" style="background:white;"><small>Sampai</small></i></div>
                      <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                      <input type="text" autocomplete="off" class="form-control " required value="<?php if ($_GET['periode'] == 'y') {
                                                                                                    echo $tanggal2;
                                                                                                  } else {
                                                                                                    echo "";
                                                                                                  } ?>" id="tanggal2" name="tanggal2">
                    </div>
                  </div>

                  <div class="col-md-6 col-sm-12 col-xs-12" style="padding: 4px;">
                    <button type="submit" class="btn btn-primary btn-sm">Pilih</button>
              </form>


            </div>

            <div class="x_content">


              
            </div>
            
            
            <div class="x_content">
                
                <table id="" class="table table-bordered" style="border-color:black; color:black">
                  <thead>
                  <tr>
                    <th colspan="6" for="namadivisi" style="color:black; text-align: center"><?php echo "periode tahun $thn"; ?></th>
                  </tr>
                </thead>
                </table>
                
                <?php
                $thn = 2025;
                 $query_divisi = mysqli_query($conn, "SELECT * FROM jenis_divisi order by id_divisi ASC ");
                 
                 
                 while ($row_divisi = mysqli_fetch_array($query_divisi)) {
                
                ?>

<table id="" class="table table-bordered" style="border-color:black; color:black">
    <thead>
        <tr>
            <th colspan="<?php echo 12 + 4; ?>" for="namadivisi" style="color:black; text-align: center">
                <?php echo $row_divisi['nama_divisi']; ?> - Tahun <?php echo $thn; ?>
            </th>
        </tr>
    </thead>
    <thead>
        <tr>
            <th width="15px">NO.</th>
            <th width="15px">Kode</th>
            <th width="15px">Jenis</th>
            <th>Keterangan</th>
            <?php
            // Add month columns
            for ($i = 1; $i <= 12; $i++) {
                echo "<th>" . date('M', mktime(0, 0, 0, $i, 1)) . "</th>";
            }
            ?>
            <th>Total</th>
            
        </tr>
    </thead>
    <tbody>
        <?php
        $no = 0;
        $query = mysqli_query($conn, "SELECT * FROM jenis_transaksi 
                                    WHERE id_jenis NOT IN (1) 
                                    AND id_divisi='$row_divisi[id_divisi]' 
                                    ORDER BY jenis, nama_jenis ASC");

        // Initialize grand totals array
        $grand_totals = array_fill(1, 12, 0);
        $overall_total = 0;

        while ($row = mysqli_fetch_array($query)) {
            $no++;
            $monthly_totals = array_fill(1, 12, 0);
            $year_total = 0;
            
            // Get all transactions for this type in the selected year
            $query_transactions = mysqli_query($conn, "SELECT 
                MONTH(tanggal) as bulan,
                SUM(debit) as total_debit,
                SUM(kredit) as total_kredit
                FROM transaksi 
                WHERE id_jenis = '$row[id_jenis]'
                AND YEAR(tanggal) = '$thn'
                GROUP BY MONTH(tanggal)");
            
            while ($transaction = mysqli_fetch_array($query_transactions)) {
                $month = $transaction['bulan'];
                $amount = ($row['jenis'] == "0") ? $transaction['total_debit'] : $transaction['total_kredit'];
                
                $monthly_totals[$month] = $amount;
                $year_total += $amount;
                
                // Add to grand totals
                $grand_totals[$month] += $amount;
                $overall_total += $amount;
            }
        ?>
            <tr style="background: <?php echo $row['jenis']=='0' ? '#eefc6f' : ($no % 2 == 1 ? 'white' : 'white'); ?>">
                <td><?php echo $no; ?></td>
                <td><?php echo $row['id_jenis']; ?></td>
                <td><h4><?php echo $row['jenis']=="0" ? "Pemasukan" : "Pengeluaran"; ?></h4></td>
                <td><h4><?php echo $row['nama_jenis']; ?></h4></td>
                
                <?php
                // Display monthly amounts
                for ($month = 1; $month <= 12; $month++) {
                    echo "<td><h4>" . ($monthly_totals[$month] != 0 ? rupiah($monthly_totals[$month]) : '-') . "</h4></td>";
                }
                ?>
                
                <td><h4><?php echo rupiah($year_total); ?></h4></td>
               
            </tr>
        <?php } ?>
        
        <!-- Total row -->
        <tr bgcolor="#f6eee0">
            <td colspan="4" align="center"><b>TOTAL</b></td>
            <?php
            // Display monthly grand totals
            for ($month = 1; $month <= 12; $month++) {
                echo "<td><h4>" . ($grand_totals[$month] != 0 ? rupiah($grand_totals[$month]) : '-') . "</h4></td>";
            }
            ?>
            <td><h4><?php echo rupiah($overall_total); ?></h4></td>
            
        </tr>
    </tbody>
</table>


              <br><br>
              <?php
              
                 }
              
              if ($_GET['periode'] == 'y') {
                echo "<a href='component/com_transaksi/export.php?tgl1=$tgl1&tgl2=$tgl2' class='btn btn-primary btn-sm'><i class='fa fa-print'></i> Cetak</a>";
              } else {
                echo "<a href='component/com_transaksi/export.php?bln=$bln&thn=$thn' class='btn btn-primary btn-sm'><i class='fa fa-print'></i> Cetak</a>";
              }
              ?>
            </div>










          </div>

        </div>

      </div>
    </div>
  </div>
  <!-- /page content --><br>










          </div>

        </div>

      </div>
    </div>
  </div>
  <!-- /page content --><br>







<?php } ?>